Note: This script randomly selections 1000 postcodes in London and 1000 postcodes outside London. It is only needed if you don't have the staff data yourself!


In [1]:
import pandas as pd 
from mylibrary.connections import cursor, conn, engine, Automapped_Base, session
sql = """
drop table if exists tt_gh.staff_locations;
create table  tt_gh.staff_locations as
(select postcode, s.geom from tt_gh.all_postcodes as s, tt_gh.gb_and_london as g
where st_contains(g.geom,s.geom)  and g.name='london'
order by random()
limit 1000)

union all

(select postcode, s.geom from tt_gh.all_postcodes as s, tt_gh.gb_and_london as g
where not st_contains(g.geom,s.geom)  and g.name='london'
order by random()
limit 1000);

CREATE INDEX idx_geom_staff_locations ON tt_gh.staff_locations USING gist(geom);
"""
cur = conn.cursor()
cur.execute(sql)
conn.commit()


/Users/robinlinacre/anaconda/lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/base.py:2505: SAWarning: Did not recognize type 'geometry' of column 'geom'
  (attype, name))